import java.util.Scanner;

public class Manager {

    static Scanner sc = new Scanner(System.in);

    static String[][] users = new String[50][3];
    static String[][] books = new String[50][5];
    static String[][] pubs = new String[50][5];

    public static void main(String[] args) {
        System.out.println("欢迎来到龙院图书管理系统！\n" + "1.登录 2.注册");
        int logorreg = sc.nextInt();
        switch (logorreg){
            case 1:
                login();
                break;
            case 2:
                register();
                break;
            default:
                break;
        }
    }

    private static void homePage() {
        System.out.println("欢迎来到龙院图书管理系统！\n" + "1.登录 2.注册");
        int logorreg = sc.nextInt();
        switch (logorreg) {
            case 1:
                login();
                break;
            case 2:
                register();
                break;
            default:
                break;
        }
    }

    private static void register() {
        System.out.println("请输入所属部门：");
        String deptName = sc.next();
        System.out.println("请输入用户名：");
        String useName = sc.next();
        System.out.println("请输入密码：");
        String passWord = sc.next();

        for (int i = 0; i < users.length; i++) {
            if (users[i][0] == null){
                users[i][0] = deptName;
                users[i][1] = useName;
                users[i][2] = passWord;
                System.out.println("注册成功！");
                break;
            }
        }
        homePage();
    }

    private static void LibraryManagement() {
        System.out.println("请输入：1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
        int secondChoice = sc.nextInt();
        if (secondChoice == 1) {
            int p = 0;
            add(books,p);
        } else if (secondChoice == 2) {
            del();
        } else if (secondChoice == 3) {
            update();
        } else if (secondChoice == 4) {
            select();
        } else if (secondChoice == 5) {
//            previousPage();
            homePage();
        } else {
            System.out.println("输入错误");
        }
    }

    private static void add(String[][] books, int i) {
        System.out.println("请输入图书ISBN:");
        String isbn = sc.next();
        books[i][0] = isbn;
        System.out.println("请输入书名:");
        String bookName = sc.next();
        books[i][1] = bookName;
        System.out.println("请输入价格:");
        String bookPrice = sc.next();
        books[i][2] = bookPrice;
        System.out.println("请输入出版社:");
        String pubCom = sc.next();
        books[i][3] = pubCom;
        System.out.println("请输入作者:");
        String author = sc.next();
        books[i][4] = author;

//        boolean b = insertBook(isbn, bookName, bookPrice, pubCom, author);
//        if (b) System.out.println("添加成功！");
        System.out.println("添加成功！");
        LibraryManagement();
    }

    private static boolean insertBook(String isbn, String bookName, String bookPrice, String pubCom, String author) {
        boolean b = false;
        int i = getIndexByISBN(isbn);
        if(i == -1) {
            int index = getBookIndexOfFirstNull();
            books[index][0] = isbn;
            books[index][1] = bookName;
            books[index][2] = bookPrice;
            books[index][3] = pubCom;
            books[index][4] = author;
            b = true;
        }
        return b;
    }

    private static int getBookIndexOfFirstNull() {
        int index = -1;
        int i = 0;
        do {
            if(i >= books.length) {
                break;
            }
            if(books[i][0] == null) {
                index = i;
                break;
            }
            i++;
        } while(true);
        return index;
    }

    private static int getIndexByISBN(String isbn) {
        int index = -1;
        int i = 0;
        do {
            if(i >= books.length) {
                break;
            }
            if(isbn.equals(books[i][0])) {
                index = i;
                break;
            }
            i++;
        } while(true);
        return index;
    }

    private static void select() {
        for (boolean y = true; y;){
            System.out.println("请输入查询种类：1.isbn 2.书名（模糊） 3.出版社 4. 作者 5. 价格范围 6.查询所有 7.返回上一级");
            int thirdChoice = sc.nextInt();
            if (thirdChoice == 1){
                System.out.println("请输入ISBN号：");
                String isbn = sc.next();
                ByISBN(isbn);
            }else if (thirdChoice == 2){
                System.out.println("请输入书名：");
                String bookName = sc.next();
                ByBookName(bookName);
            }else if (thirdChoice == 3){
                System.out.print("请输入出版社前的数字进行选择：");
                printPC();
                int d = sc.nextInt();
                if(d >= 1 && d <= getNumOfPC()) {
                    String pcName = pubs[d - 1][0];
                    searchBookByPC(pcName);
                } else {
                    System.out.println("请选择正确的数字！！！");
                }
            }else if (thirdChoice == 4){
                System.out.println("请输入作者姓名：");
                String author = sc.next();
                BookByAuthor(author);
            }else if (thirdChoice == 5){
                System.out.println("请输入最低价格");
                double min = sc.nextDouble();
                System.out.println("请输入最高价格");
                double max = sc.nextDouble();
                BookByPrice(min, max);
            }else if (thirdChoice == 6){
                selectall();
            }else if (thirdChoice == 7){
                LibraryManagement();
            }else {
                System.out.println("请输入正确的数字选项！！！");
            }
        }
    }

    private static void BookByPrice(double min, double max) {
        System.out.println("ISBN\t书名\t价格\t出版社\t作者\t");
        for(int i = 0; i < books.length; i++) {
            if(books[i][0] == null) {
                continue;
            }
            double price = Double.parseDouble(books[i][2]);
            if(price >= min && price <= max)
                System.out.println(books[i][0]
                        + "\t" + books[i][1]
                        + "\t" + books[i][2]
                        + "\t" + books[i][3]
                        + "\t" + books[i][4] + "\t");
        }
    }

    private static void BookByAuthor(String author) {
        System.out.println("\tISBN\t书名\t价格\t出版社\t作者\t");
        for(int i = 0; i < books.length; i++) {
            if(author.equals(books[i][4])) {
                System.out.println("\t" + books[i][0]
                        + "\t" + books[i][1]
                        + "\t" + books[i][2]
                        + "\t" + books[i][3]
                        + "\t" + books[i][4] + "\t");
            }
        }
    }

    static void searchBookByPC(String pcName) {
        System.out.println("\tISBN\t书名\t价格\t出版社\t作者\t");
        for(int i = 0; i < books.length; i++) {
            if(pcName.equals(books[i][3])) {
                System.out.println("\t" + books[i][0]
                        + "\t" + books[i][1]
                        + "\t" + books[i][2]
                        + "\t" + books[i][3]
                        + "\t" + books[i][4] + "\t");
            }
        }
    }

    static int getNumOfPC() {
        int count = 0;
        for(int i = 0; i < pubs.length; i++) {
            if(pubs[i][0] != null) {
                count++;
            }
        }
        return count;
    }

    static void printPC() {
        for(int i = 0; i < pubs.length; i++) {
            if(pubs[i][0] != null) {
                System.out.print((i + 1) + ". " + pubs[i][0] + " ");
            }
        }
        System.out.println();
    }

    private static void ByBookName(String bookName) {
        System.out.println("\tISBN\t书名\t价格\t出版社\t作者\t");
        for(int i = 0; i < books.length; i++) {
            if(books[i][0] != null && books[i][1].contains(bookName)) {
                System.out.println("\t" + books[i][0]
                        + "\t" + books[i][1]
                        + "\t" + books[i][2]
                        + "\t" + books[i][3]
                        + "\t" + books[i][4] + "\t");
            }
        }
    }

    private static void ByISBN(String isbn) {
        System.out.println("ISBN\t书名\t价格\t出版社\t作者\t");
        int i = 0;
        do {
            if(i >= books.length) {
                break;
            }
            if(isbn.equals(books[i][0])) {
                System.out.println(books[i][0]
                        + "\t" + books[i][1]
                        + "\t" + books[i][2]
                        + "\t" + books[i][3]
                        + "\t" + books[i][4] + "\t");
                break;
            }
            i++;
        } while(true);
    }

    private static void selectall() {
        System.out.println("ISBN\t书名\t价格\t出版社\t作者\t");
        for (int i = 0; i < books.length; i++)
            if (books[i][0] != null) {
                for (int j = 0; j < books[i].length; j++) {
                    System.out.print(books[i][j] + "   ");
                }
                System.out.println();
            }
    }

    private static void update() {
        System.out.println("请输入ISBN号：");
        String isbn = sc.next();
        int index = getIndexByISBN(isbn);
        if(index != -1) {
            System.out.println("请输入新的书名：");
            String bookName = sc.next();
            System.out.println("请输入新的价格：");
            String bookPrice = sc.next();
            System.out.println("请输入新的出版社：");
            String pubCom = sc.next();
            System.out.println("请输入新的作者：");
            String author = sc.next();
            boolean b = updateBook(index, bookName, bookPrice, pubCom, author);
            if (!b) {
                System.out.println("更新失败");
            } else {
                System.out.println("更新成功！！！");
            }
        } else {
            System.out.println("该ISBN号不存在！！！");
        }
    }

    private static boolean updateBook(int index, String bookName, String bookPrice, String pubCom, String author) {
        boolean b = false;
        books[index][1] = bookName;
        books[index][2] = bookPrice;
        books[index][3] = pubCom;
        books[index][4] = author;
        b = true;
        return b;
    }

    private static boolean updatePubCom(int index, String pcAddress, String pcPerson) {
        boolean b = false;
        pubs[index][1] = pcAddress;
        pubs[index][2] = pcPerson;
        b = true;
        return b;
    }

    private static void printPubCom(int index) {
        System.out.println("出版社名称地址\t联系人\t");
        System.out.print(pubs[index][0] + "\t");
        System.out.print(pubs[index][1] + "\t");
        System.out.println(pubs[index][2] + "\t");
    }

    private static void del() {
        System.out.println("请输入要删除的出版社名称：");
        String pcName = sc.next();
        boolean b = delByPCName(pcName);
        if(b) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    private static boolean delByPCName(String pcName) {
        boolean b = false;
        int index = getPubIndex(pcName);
        if(index != -1) {
            if(!isExistInBook(pcName)) {
                for(int i = index; i < pubs.length - 1; i++)
                    pubs[i] = pubs[i + 1];
                b = true;
            } else {
                System.out.println("该出版社有相关图书信息存在！不可以删除！如果要删除请先删除所有相关图书信息！！！");
            }
        }
        return b;
    }

    private static boolean isExistInBook(String pcName) {
        boolean bool = false;
        int i = 0;
        do {
            if(i >= books.length) {
                break;
            }
            if(pcName.equals(books[i][3])) {
                bool = true;
                break;
            }
            i++;
        } while(true);
        return bool;
    }

    private static int getPubIndex(String pcName) {
        int index = -1;
        int i = 0;
        do {
            if(i >= pubs.length) {
                break;
            }
            if(pcName.equals(pubs[i][0])) {
                index = i;
                break;
            }
            i++;
        } while(true);
        return index;
    }

    private static void PublishingHouseManagement() {
        System.out.println("请输入：1.增加 2.删除 3.更新 4.根据出版社名称查询 5.查询所有出版社 6.返回上一级菜单");
        int key = sc.nextInt();
        switch(key) {
            case 1:
                addPub();
                break;

            case 2:
                delPub();
                break;

            case 3:
                updatePub();
                break;

            case 4:
                searchPubByName();
                break;

            case 5:
                searchAllPubCom();
                break;

            case 6:
                homePage();
                break;
        }
    }

    private static void searchAllPubCom() {
        System.out.println("出版社名称\t地址\t联系人\t");
        for(int i = 0; i < pubs.length; i++) {
            if(pubs[i][0] != null) {
                System.out.println(pubs[i][0]
                        + "\t" + pubs[i][1]
                        + "\t" + pubs[i][2] + "\t");
            }
        }
    }

    private static void searchPubByName() {
        System.out.println("请输入出版社名称：");
        String pcName = sc.next();
        int index = getPubIndex(pcName);
        if(index != -1) {
            System.out.println("出版社名称：" + pubs[index][0]);
            System.out.println("出版社地址：" + pubs[index][1]);
            System.out.println("出版社联系人：" + pubs[index][2]);
        } else {
            System.out.println("该出版社不存在！！！");
        }
    }

    private static void updatePub() {
        System.out.println("请输入要更新的出版社名称：");
        String pubName = sc.next();
        int index = getPubIndex(pubName);
        printPubCom(index);
        System.out.println("请输入要更新的地址：");
        String pcAddress = sc.next();
        System.out.println("请输入要更新的联系人姓名：");
        String pcPerson = sc.next();
        boolean b = updatePubCom(index, pcAddress, pcPerson);
        if(b) {
            System.out.println("更新成功！");
            printPubCom(index);
        } else {
            System.out.println("更新失败！！！");
        }
    }

    private static void delPub() {
        System.out.println("请输入要删除的出版社名称：");
        String pubName = sc.next();
        boolean boo = delByPCName(pubName);
        if(boo) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    private static void addPub() {
        System.out.println("请输入出版社名称：");
        String pcName = sc.next();
        System.out.println("请输入出版社地址：");
        String pcAddress = sc.next();
        System.out.println("请输入出版社联系人：");
        String pcPerson = sc.next();
        boolean b = insertPC(pcName, pcAddress, pcPerson);
        if(b) {
            System.out.println("出版社添加成功");
        } else {
            System.out.println("出版社添加失败");
        }
    }

    private static boolean insertPC(String pcName, String pcAddress, String pcPerson) {
        boolean b = false;
        if(getPubIndex(pcName) == -1) {
            int index = getPCIndexOfFirstNull();
            pubs[index][0] = pcName;
            pubs[index][1] = pcAddress;
            pubs[index][2] = pcPerson;
            b = true;
        }
        return b;
    }

    private static int getPCIndexOfFirstNull() {
        int index = -1;
        int i = 0;
        do {
            if(i >= pubs.length) {
                break;
            }
            if(pubs[i][0] == null) {
                index = i;
                break;
            }
            i++;
        } while(true);
        return index;
    }

//    private static void LogOut() {
//    }

    private static void ExitSystem() {
        System.out.println("欢迎下次光临");
        System.exit(0);
    }

    private static void firstPage() {
        System.out.println("欢迎您使用龙院书籍管理系统！！！");
        System.out.println("请输入数字进行选择：1 图书管理  2 出版社管理  3 退出登录  4 退出系统");
        int firstChoice = sc.nextInt();

        switch (firstChoice) {
            case 1:
                LibraryManagement();
                break;
            case 2:
                PublishingHouseManagement();
                break;
            case 3:
//                LogOut();
                homePage();
                break;
            case 4:
                ExitSystem();
        }
    }

    private static void login() {
        System.out.println("请输入用户名：");
        String username = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();

        boolean flag = false;
        // 判断登录是否成功
        for (int i = 0; i < users.length; i++) {
            if (users[i][1] != null && users[i][1].equals(username) && users[i][2].equals(password)) {
                System.out.println("登录成功！");
                firstPage();
            } else {
                System.out.println("用户错误！");
            }
            flag = true;
            break;
        }
    }
}